package com.example.liuxing.jdmall.controller;

import android.content.Context;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.liuxing.jdmall.bean.Banner;
import com.example.liuxing.jdmall.bean.RGetYourFav;
import com.example.liuxing.jdmall.bean.RResult;
import com.example.liuxing.jdmall.bean.RSecoundKill;
import com.example.liuxing.jdmall.constant.IdiyMessage;
import com.example.liuxing.jdmall.constant.NetworkConstant;
import com.example.liuxing.jdmall.utils.NetWorkUtils;

import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liuxing on 2017/7/25.
 */

public class HomeController extends BaseController {

    public HomeController(Context context) {
        super(context);
    }

    @Override
    protected void handleMessage(int action, Object... values) {
        switch (action) {
            case IdiyMessage.GET_BANNER_ACTION:
                ArrayList<Banner> banners = loadBanner((int) values[0]);
                modeChangeListener.onModelChanged(IdiyMessage.GET_BANNER_ACTION_RESULT, banners);
                break;
            case IdiyMessage.SECOND_KILL_ACTION:
                modeChangeListener.onModelChanged(IdiyMessage.SECOND_KILL_ACTION_RESULT, loadSecondKillData());
                break;
            case IdiyMessage.RECOMMEND_ACTION:
                modeChangeListener.onModelChanged(IdiyMessage.RECOMMEND_ACTION_RESULT, loadRecommendData());
                break;
        }
    }

    private ArrayList<RGetYourFav> loadRecommendData() {
        String json = NetWorkUtils.doGet(NetworkConstant.LOAD_RECOMMEND_PRODUCT);
        RResult jsonResult = JSON.parseObject(json, RResult.class);
        if (jsonResult.isSuccess()) {
            try {
                org.json.JSONObject jsonObject = new org.json.JSONObject(jsonResult.getResult());
                String rowsJson = jsonObject.getString("rows");
                Log.v("SecondKillData", rowsJson);
                return (ArrayList<RGetYourFav>)JSON.parseArray(rowsJson, RGetYourFav.class);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return new ArrayList<RGetYourFav>();
    }

    private ArrayList<RSecoundKill> loadSecondKillData() {
        String json = NetWorkUtils.doGet(NetworkConstant.SECONDKILL_URL);
        RResult jsonResult = JSON.parseObject(json, RResult.class);
        if (jsonResult.isSuccess()) {
            try {
                org.json.JSONObject jsonObject = new org.json.JSONObject(jsonResult.getResult());
                String rowsJson = jsonObject.getString("rows");
//                Log.v("SecondKillData", rowsJson);
                return (ArrayList<RSecoundKill>)JSON.parseArray(rowsJson, RSecoundKill.class);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return new ArrayList<RSecoundKill>();
    }

    private ArrayList<Banner> loadBanner(int type) {
        ArrayList<Banner> result = new ArrayList<Banner>();
        String url = NetworkConstant.BANNER_URL + "?adkind=" + type;
        String json = NetWorkUtils.doGet(url);
        RResult jsonResult = JSON.parseObject(json, RResult.class);
        if (jsonResult.isSuccess()) {
            String ruslut1 = jsonResult.getResult();
            return (ArrayList<Banner>) JSON.parseArray(ruslut1, Banner.class);
        }
        return result;
    }

}
